<template>
	<view class="uni-container">
		<uni-forms ref="form" :model="formData" validate-trigger="submit" err-show-type="toast">
			<uni-forms-item name="ActivityName" label="活动名称">
				<uni-easyinput placeholder="活动的名称或主题" v-model="formData.ActivityName" trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="ActivityDateTime_start" label="开始时间">
				<uni-datetime-picker return-type="timestamp"
					v-model="formData.ActivityDateTime_start"></uni-datetime-picker>
			</uni-forms-item>
			<uni-forms-item name="use_time" label="使用时段">
				<uni-data-checkbox v-model="formData.use_time"
					:localdata="formOptions.use_time_localdata"></uni-data-checkbox>
			</uni-forms-item>
			<uni-forms-item name="use_position" label="使用场地">
				<uni-easyinput placeholder="使用场地" v-model="formData.use_position" trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="use_department" label="使用部门">
				<uni-easyinput placeholder="使用部门" v-model="formData.use_department" trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="use_people" label="受众人数">
				<uni-easyinput placeholder="受众人数" v-model="formData.use_people" trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="user" label="负责人">
				<uni-data-picker v-model="formData.user" collection="uni-id-users"
					field="nickname as text, _id as value"></uni-data-picker>
			</uni-forms-item>
			<uni-forms-item name="user_phone" label="负责人电话">
				<uni-data-picker v-model="formData.user_phone" collection="uni-id-users"
					field="username as text, _id as value"></uni-data-picker>
			</uni-forms-item>
			<uni-forms-item name="shenhe_advice" label="审核意见" v-show="false">
				<uni-easyinput placeholder="审核意见" v-model="formData.shenhe_advice" trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="shehe_status" label="审核状态" v-show="false">
				<uni-data-checkbox v-model="formData.shehe_status"
					:localdata="formOptions.shehe_status_localdata"></uni-data-checkbox>
			</uni-forms-item>
			<uni-forms-item name="Notes" label="备注">
				<uni-easyinput placeholder="关于活动的额外信息或特殊要求" v-model="formData.Notes" trim="both"></uni-easyinput>
			</uni-forms-item>
			<view class="uni-button-group">
				<button type="primary" class="uni-button" @click="submit">提交</button>
			</view>
		</uni-forms>
	</view>
</template>

<script>
	import {
		validator
	} from '../../js_sdk/validator/abu_activity_yuyue.js';

	const db = uniCloud.database();
	const dbCollectionName = 'abu_activity_yuyue';

	function getValidator(fields) {
		let result = {}
		for (let key in validator) {
			if (fields.indexOf(key) > -1) {
				result[key] = validator[key]
			}
		}
		return result
	}



	export default {
		data() {
			let formData = {
				"ActivityName": "",
				"ActivityDateTime_start": null,
				"use_time": null,
				"use_position": "",
				"use_department": "",
				"use_people": "",
				"user": "",
				"user_phone": "",
				"shenhe_advice": "",
				"shehe_status": null,
				"Notes": ""
			}
			return {
				formData,
				formOptions: {
					"use_time_localdata": [{
							"text": "上午(09:00:12:00)",
							"value": 0
						},
						{
							"text": "下午(14:00:15:00)",
							"value": 1
						}
					],
					"shehe_status_localdata": [{
							"text": "通过",
							"value": 0
						},
						{
							"text": "驳回",
							"value": 1
						}
					]
				},
				rules: {
					...getValidator(Object.keys(formData))
				}
			}
		},
		onLoad() {
			const user_uid = uniCloud.getCurrentUserInfo()
			console.log(user_uid);
			this.formData.user_phone = user_uid.uid
			this.formData.user = user_uid.uid

			// db.collection("uni-id-users").where({
			// 	_id: user_uid.uid
			// }).field("nickname").get().then(res => {
			// 	console.log(res);
			// 	this.formData.user = res.result.data.nickname === "" ? "未设置，请在设置里面设置" : res.result.data.nickname
			// })
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {

			/**
			 * 验证表单并提交
			 */
			submit() {
				uni.showLoading({
					mask: true
				})
				this.$refs.form.validate().then((res) => {
					return this.submitForm(res)
				}).catch(() => {}).finally(() => {
					uni.hideLoading()
				})
			},

			/**
			 * 提交表单
			 */
			submitForm(value) {
				// 使用 clientDB 提交数据
				return db.collection(dbCollectionName).add(value).then((res) => {
					uni.showToast({
						icon: 'none',
						title: '新增成功'
					})
					this.getOpenerEventChannel().emit('refreshData')
					setTimeout(() => uni.navigateBack(), 500)
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				})
			}
		}
	}
</script>

<style>
	.uni-container {
		padding: 15px;
	}

	.uni-input-border,
	.uni-textarea-border {
		width: 100%;
		font-size: 14px;
		color: #666;
		border: 1px #e5e5e5 solid;
		border-radius: 5px;
		box-sizing: border-box;
	}

	.uni-input-border {
		padding: 0 10px;
		height: 35px;

	}

	.uni-textarea-border {
		padding: 10px;
		height: 80px;
	}

	.uni-button-group {
		margin-top: 50px;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		justify-content: center;
	}

	.uni-button {
		width: 184px;
	}
</style>